Date.prototype.format = function (conf = 'YYMMDD', type = 1) {
  var y = this.getFullYear()
  var m = this.getMonth() + 1 // 月份从0开始算
  m = m < 10 ? '0' + m : m
  var d = this.getDate()
  d = d < 10 ? '0' + d : d
  var h = this.getHours()
  h = h < 10 ? '0' + h : h
  var f = this.getMinutes()
  f = f < 10 ? '0' + f : f
  var s = this.getSeconds()
  s = s < 10 ? '0' + s : s
  if (conf === 'YYMMDD') {
    return y + '-' + m + '-' + d
  } else if (conf === 'YYMMDD hh:mm') {
    return y + '-' + m + '-' + d + ' ' + h + ':' + f
  } else if (conf === 'YYMMDD hh:mm:ss') {
    return y + '-' + m + '-' + d + ' ' + h + ':' + f + ':' + s
  } else if (conf === 'MMDD hh:mm') {
    return m + '-' + d + ' ' + h + ':' + f
  } else if (conf === 'hh:mm') {
    if (type === 1) {
      return h + ':' + f
    } else { // 此处为我的消息时间显示而进行的判断
      var ynow = new Date().getFullYear()
      var dnow = new Date().getDate()
      dnow = dnow < 10 ? '0' + dnow : dnow
      if (ynow === y && dnow === d) {
        return h + ':' + f
      } else if (ynow === y && dnow !== d) {
        return m + '-' + d + ' ' + h + ':' + f
      } else if (ynow !== y) {
        return y + '-' + m + '-' + d + ' ' + h + ':' + f
      }
    }
  } else if (conf === 'YMMDD') {
    return y.toString().slice(-2) + '-' + m + '-' + d
  }
}

Date.prototype.formatDay = function (type = 1) {
  let day = this.getDay()
  switch (day) {
    case 0 :
      return type === 1 ? '周日' : '星期日'
    case 1 :
      return type === 1 ? '周一' : '星期一'
    case 2 :
      return type === 1 ? '周二' : '星期二'
    case 3 :
      return type === 1 ? '周三' : '星期三'
    case 4 :
      return type === 1 ? '周四' : '星期四'
    case 5 :
      return type === 1 ? '周五' : '星期五'
    case 6 :
      return type === 1 ? '周六' : '星期六'
  }
}

Date.prototype.secToday = function (sec) {
  sec = parseInt(sec)
  var now = Date.now()
  var psec = new Date(new Date().format().replace(/-/g, '/')).getTime()
  var nsec = psec + 60 * 60 * 24 * 1000
  var preDate = new Date(sec)
  var day = preDate.formatDay()
  var m = preDate.getMonth() + 1
  var d = preDate.getDate()
  if (sec >= psec && sec < nsec) {
    return '今天 ' + m + '月' + d + '日' + ' ' + day
  } else if (sec >= psec - 24 * 60 * 60 * 1000 && sec < psec) {
    return '昨天 ' + m + '月' + d + '日' + ' ' + day
  } else if (sec >= nsec && sec < nsec + 60 * 60 * 24 * 1000) {
    return '明天 ' + m + '月' + d + '日' + ' ' + day
  } else {
    return m + '月' + d + '日' + ' ' + day
  }
}

Date.prototype.dayHour = function (sec) {
  sec = parseInt(sec)
  var now = Date.now()
  var psec = new Date(new Date().format().replace(/-/g, '/')).getTime()
  var nsec = psec + 60 * 60 * 24 * 1000
  var preDate = new Date(sec)
  var day = preDate.formatDay()
  var m = preDate.getMonth() + 1
  var d = preDate.getDate()
  var h = preDate.getHours()
  var minutes = preDate.getMinutes()
  if (minutes < 10) {
    minutes = '0' + minutes
  }
  if (h < 10) {
    h = '0' + h
  }
  if (sec >= psec && sec < nsec) {
    return '今天 ' + h + ':' + minutes
  } else if (sec >= psec - 24 * 60 * 60 * 1000 && sec < psec) {
    return '昨天 ' + h + ':' + minutes
  } else if (sec >= nsec && sec < nsec + 60 * 60 * 24 * 1000) {
    return '明天 ' + h + ':' + minutes
  } else {
    return m + '月' + d + '日' + ' ' + h + ':' + minutes
  }
}

Date.prototype.formatDatePass = function () {
  // 格式化日期转化成时间戳，考虑浏览器兼容性 yyyy-MM-dd hh:mm:ss 改成 yyyy/MM/dd hh:mm:ss
  let time = this.getTime()
  let sec = (new Date().getTime() - time) / 1000
  let m = sec / 60
  let h = m / 60
  let d = h / 24

  if (d >= 1) {
    // 如果是昨天,显示昨天 否则显示日期
    // console.log(time)
    // console.log(this.getThisYearStartTime())
    if (time > this.getYesterdayStartTime()) {
      return '昨天'
    } else if (time > this.getThisYearStartTime()) {
      // 今年
      return this.formatDate(new Date(time), 'MM-dd')
    } else {
      // 去年
      return this.formatDate(new Date(time), 'yyyy-MM-dd')
    }
  } else if (h >= 1) {
    return parseInt(h) + '小时前'
  } else if (m >= 1) {
    return parseInt(m) + '分钟前'
  } else {
    return parseInt(sec) + '秒前'
    // return '刚刚'
  }
}

Date.prototype.fromToday = function (tsp) {
  var now = Date.now()
  var sec = (parseInt(tsp) - now) / 1000
  var d = Math.floor(sec / (60 * 60 * 24))
  if (d) {
    return d + '天后'
  }
  var h = Math.floor(sec % (60 * 60 * 24) / (60 * 60))
  if (h) {
    return h + '小时后'
  }
  var m = Math.floor(sec % (60 * 60 * 24) % (60 * 60) / 60)
  if (m) {
    return m + '分钟后'
  }
  return '马上开始'
}

Date.prototype.getTodayStartTime = function () {
  let today = new Date()
  today.setHours(0)
  today.setMinutes(0)
  today.setSeconds(0)
  today.setMilliseconds(0)
  return today.getTime()
}

Date.prototype.getYesterdayStartTime = function () {
  let today = this.getTodayStartTime()
  let oneDay = 1000 * 60 * 60 * 24
  return today - oneDay
}

Date.prototype.getThisYearStartTime = function () {
  let today = new Date()
  today.setMonth(0)
  today.setDate(1)
  today.setHours(0)
  today.setMinutes(0)
  today.setSeconds(0)
  today.setMilliseconds(0)
  return today.getTime()
}

Date.prototype.formatDate = function (date, format) {
  let o = {
    'M+': date.getMonth() + 1,
    'd+': date.getDate(),
    'h+': date.getHours(),
    'm+': date.getMinutes(),
    's+': date.getSeconds()
  }
  if (/(y+)/.test(format)) {
    format = format.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  }
  for (var k in o) {
    if (new RegExp('(' + k + ')').test(format)) {
      format = format.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
    }
  }
  return format
}
